import { StationNetwork } from "GameLoop/Path/StationNetwork";
import { EntityDB } from "../../../EntityManager/EntityDB";
import { ETeamState } from "../../../EntityManager/Entitys/team";
import { PollingStationManager } from "../../../PollingStation/PollingStationManager";

  interface WorldGetter
{
    EntityDB:EntityDB
    Station:PollingStationManager
    registerNotify(region:EventRegion,eventtype:INotifyEvent):void
}

/**
 * 状态更新
 */
export class StateUpdater
{
    public static  updateCreepRoleStatus(worldgetter:WorldGetter, room:Required<Room>,emitter:TEventEmitter<TEventTypes>)
    {
        if(room.canExecute(100))
        {
            for(let k in room.memory.roleStatus)
            {
                const creeps = room.memory.roleStatus[k as RoleName];
                for(let i=0;i<creeps.length;)
                {
                    if(!Game.creeps[creeps[i]])
                    {
                        creeps.splice(i,1);
                    }
                    else
                    {
                        i++;
                    }
                }
                room.memory.roleStatus[k as RoleName] =Array.from( new Set(creeps));
            }
        }
        
    }
    public static  updateEntity(worldgetter:WorldGetter, room:Required<Room>,emitter:TEventEmitter<TEventTypes>)
    {
        room.getEntitys("Team4")?.forEach(e=>
            {
                e.updateMemory();
                if(e.getNodeMemory().teamState == ETeamState.Dead)
                {
                    worldgetter.EntityDB.removeEntityByID(e.GUID())
                }
            });
        room.getEntitys("Team2")?.forEach(e=>
            {
                e.updateMemory();
                if(e.getNodeMemory().teamState == ETeamState.Dead)
                {
                    worldgetter.EntityDB.removeEntityByID(e.GUID());
                    const findEntity = worldgetter.EntityDB.getEntityByID(e.GUID())
                    if(findEntity)
                    {
                        global.Log.Error(`找到了已死亡的小队${findEntity.GUID()}_${findEntity.entityType()}`);
                        Memory.Debug[`已死亡的小队_${findEntity.GUID()}`]=Game.time;
                    }
                }
            });

    }

        /**
     * 要在tick的结尾来更新
     * @param room 
     */ 
    public static  checkUpdateRoomHistory(worldgetter:WorldGetter, room:Required<Room>,emitter:TEventEmitter<TEventTypes>)
    {
        // 升级事件触发2tick
        
        room.memory.history=
        {
            time:Game.time,
            level:room.controller?.level as number,
            energyCapacity:room.energyCapacityAvailable,
        }
    }
    public static  checkUpdateRoomScan(worldgetter:WorldGetter, room:Required<Room>,emitter:TEventEmitter<TEventTypes>)
    {
        // 升级事件触发2tick
        
        room.memory.lst = Game.time;
        (room.memory as any).lastScanTime = undefined
    }
    /**
     * 检测更新当前房间的冗余资源
     * */ 
    public static checkResourceStats(worldgetter:WorldGetter, room:Required<Room>,emitter:TEventEmitter<TEventTypes>)
    {
        // const coreentity = room.getEntity("CoreEntity"); 
        // if(global.canExecute(1000))   
        // {
        //     {
        //         const res = coreentity.getSurplusResource();
        //         const keys = Object.keys(res) as ResourceConstant[];
        //         for(const e of keys)
        //         {
        //             room.registerExcessResource(e,res[e] as number);
        //         }            
        //     }
    
        //     {
        //         const res = coreentity.getShortageResource();
        //         const keys = Object.keys(res) as ResourceConstant[];
        //         for(const e of keys)
        //         {
        //             room.registerShortageResource(e,res[e] as number);
        //         }            
        //     }
        // }
    }

}